/**
	License:		Artistic license 2.0 (see license.txt, also available online at http://www.perlfoundation.org/artistic_license_2_0)
	Authors:		OrbitalLab (http://www.orbitallab.ru/moonglide/), 2007-2009

	File:			ResTexture.d
	Description:	texture compositor
	Date:			09.04.2009 by Digited
**/
module moonglide.resources.resobjects.hilevel.ResTexture;

public import moonglide.resources.resobjects.lowlevel.ResTextureData;

import moonglide.resources.Management;

//================================================================
/// Cube Map - texture compositor
//================================================================
class CResTexture : IResource
{
	protected
		bool	_NeedToUpdate = false;
		bool	_dead;
		ubyte	_loadedS = 0;

		this( char[] id )
		{
			CResManager().GenerateUniqueName( id );
			super( id );
			_New( this );
		}

	public:
		uint	textureID = 0,	// tType may be in OGL : GL_TEXTURE_1D, GL_TEXTURE_2D,
								// GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB etc
				tType = 0;
		ubyte	textureType;

		bool	swapCube = false;

		void	setNotLoaded()		{ _loadedS = 0; }
		void	setLoading()		{ _loadedS = 1; }
		void	setLoaded()			{ _loadedS = 2; }
		bool	loaded()			{ return _loadedS == 2; }

		CResTextureData[] data;

		bool isAlive() { return !_dead; }

		void needToUpdate( bool need )
		{
			_NeedToUpdate = need;

			if( need )
				foreach( inout texdata; data )
					if( texdata )
						texdata.needToUpdate = true;
		}

		bool needToUpdate() { return _NeedToUpdate; }

//=================================================================

		static CResTexture opCall( char[] id )
		{
			return new CResTexture( id );
		}

		~this()
		{
			foreach( i, ref texdata; data )
				if( texdata )
				{
					if( texdata.isAlive )
						texdata.release;

					texdata = null;
				}
		}
}